package org.csu.mall.controller.front;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Account;
import org.csu.mall.entity.Commodity;
import org.csu.mall.service.ICommodityRateService;
import org.csu.mall.service.ICommodityService;
import org.csu.mall.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("commodity")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;

    @Autowired
    private ICommodityRateService commodityRateService;

    @GetMapping("item_detail")
    @ResponseBody
    public UniformResponse<CommodityVO> getCommodityDetail(String itemId){
        return commodityService.getCommodityDetail(itemId);
    }

    @PostMapping("commodity_list")
    @ResponseBody
    public UniformResponse<CommodityListVO> getCommodityList(
            @RequestParam(name = "categoryId", required = false) String categoryId,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "orderBy", defaultValue = "price_desc") String orderBy,
            @RequestParam(name = "source", defaultValue = "2") int source,
            @RequestParam(name = "status", defaultValue = "2") int status){
        return commodityService.getCommodityList(categoryId, keyword, pageNum, pageSize, orderBy, source, status);
    }

    @PostMapping("release_item")
    @ResponseBody
    public UniformResponse<String> releaseCommodity(Commodity commodity, String address, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
//        Account account1 = new Account();
//        account1.setUserId(1001);
//        account1.setRole(2);
        return commodityService.releaseCommodity(account, address, commodity);
    }

    @PostMapping("update_item")
    @ResponseBody
    public UniformResponse<String> updateCommodity(Commodity commodity, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
//        Account account1 = new Account();
//        account1.setUserId(1001);
//        account1.setRole(2);
        return commodityService.updateCommodity(account, commodity);
    }

    @GetMapping("cancel_item")
    @ResponseBody
    public UniformResponse<String> cancelCommodity(String itemId, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
//        Account account1 = new Account();
//        account1.setUserId(1001);
//        account1.setRole(2);
        return commodityService.cancelCommodity(account, itemId);
    }

    @GetMapping("purchase_item")
    @ResponseBody
    public UniformResponse<CartCommodityVO> purchaseItem(String itemId, HttpSession session){
        //todo 待测
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return commodityService.purchaseCommodity(itemId, account);
    }

    //------------------------获取商品评价----------------------
    @GetMapping("item_comment")
    @ResponseBody
    public UniformResponse<CommodityRateListVO> commodityComments(
            @RequestParam(name = "itemId") String itemId,
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum){
        return commodityRateService.commodityComments(itemId, pageSize, pageNum);
    }

    //新增添加图片
    @PostMapping("add_commodity_image")
    @ResponseBody
    public UniformResponse<String> addCommodityImage(String itemId, String imgAddress, HttpSession session){
        Account account = (Account)session.getAttribute(CONSTANT.LOGIN_USER);
        return commodityService.addCommodityImage(itemId, imgAddress, account);
    }
}
